import concurrent.futures
import requests
import json


# 假设这是你要请求的URL
API_URL = 'http://192.168.1.241:8888/information/policyNews/page'


Headers = {
    'Content-Type': 'application/json',
    'Authorization': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjcwMTc2NjBiLWMxOWItNGIzMC04MDk2LTk2NDU3MTUxYzU0ZiJ9.EYuIlbN2KMYQ018ZQKJM-WJlnMNYQ20IK-xOcAP-dQhQ1S7hisvG1PFcBZzKeMT165Rg9x9q2SUOijHx5xmg8Q',
    'Channel': 'admin',
    'Host': '183.95.190.238:10771'
}



# 模拟的请求参数列表，每个元素都是一个字典，代表一个JSON请求体
# 这里我们使用简单的递增ID和随机值作为示例
import random
import string



# def generate_random_string(length=10):
#     letters = string.ascii_lowercase
#     return ''.join(random.choice(letters) for i in range(length))


params_list = [{'page': 1, 'policyNewsType': 'policy', 'size': 10}]


# 定义一个函数来执行网络请求
def send_request(params):
    try:
        response = requests.post(API_URL, json=params, headers=Headers)
        response.raise_for_status()  # 如果响应状态码不是200，则抛出HTTPError异常
        return response.json()  # 假设我们关心的是响应的JSON内容
    except requests.RequestException as e:
        print(f"Error sending request: {e}")
        return None

    # 主函数


def main():
    num_requests = len(params_list)  # 并发请求的数量

    # 创建一个线程池
    with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
        # 提交所有请求到线程池
        future_to_result = {executor.submit(send_request, params): params for params in params_list}

        # 等待所有请求完成，并处理结果
        for future in concurrent.futures.as_completed(future_to_result):
            params = future_to_result[future]
            try:
                result = future.result()
                if result:
                    print(f"Result for {params}: {result}")  # 假设我们打印出整个响应结果
            except Exception as exc:
                print(f'Error for {params}: {exc}')


if __name__ == "__main__":
    main()